<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
    

    

    



    <meta charset="utf-8">
    
    
    
    <title>mapbox源码分析（一） | 奔跑的蜗牛 | 虚心若愚</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="gis,mapbox-gl-js">
    <meta name="description" content="Mapbox原码分析一、基本架构                                                                                             基本架构                1. 接口层Marker在Mapbox GL JS中Marker类似与openlayers中的overlay，以DOM元素的方式呈现信息。默认">
<meta name="keywords" content="gis,mapbox-gl-js">
<meta property="og:type" content="article">
<meta property="og:title" content="mapbox源码分析（一）">
<meta property="og:url" content="http://windynature.oschina.io/2020/03/18/mapbox/mapbox源码分析/index.html">
<meta property="og:site_name" content="奔跑的蜗牛">
<meta property="og:description" content="Mapbox原码分析一、基本架构                                                                                             基本架构                1. 接口层Marker在Mapbox GL JS中Marker类似与openlayers中的overlay，以DOM元素的方式呈现信息。默认">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://windynature.oschina.io/blog/2020/03/18/mapbox/mapbox源码分析/基本架构.png">
<meta property="og:image" content="http://windynature.oschina.io/blog/2020/03/18/mapbox/mapbox源码分析/StyleLayer子类.png">
<meta property="og:image" content="http://windynature.oschina.io/blog/2020/03/18/mapbox/mapbox源码分析/source结构.png">
<meta property="og:image" content="http://windynature.oschina.io/blog/2020/03/18/mapbox/mapbox源码分析/WrokerSource实现类.png">
<meta property="og:image" content="http://windynature.oschina.io/blog/2020/03/18/mapbox/mapbox源码分析/数据处理流程.png">
<meta property="og:image" content="http://windynature.oschina.io/blog/2020/03/18/mapbox/mapbox源码分析/数据加载与渲染流程.png">
<meta property="og:updated_time" content="2020-03-18T01:06:47.494Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="mapbox源码分析（一）">
<meta name="twitter:description" content="Mapbox原码分析一、基本架构                                                                                             基本架构                1. 接口层Marker在Mapbox GL JS中Marker类似与openlayers中的overlay，以DOM元素的方式呈现信息。默认">
<meta name="twitter:image" content="http://windynature.oschina.io/blog/2020/03/18/mapbox/mapbox源码分析/基本架构.png">
    
        <link rel="alternative" href="/atom.xml" title="奔跑的蜗牛" type="application/atom+xml">
    
    <link rel="shortcut icon" href="/blog/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@1.5.2/css/style.css">
    <script>window.lazyScripts=[]</script>
</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/blog/img/timg.jpg)">
      <div class="brand">
        <a href="/blog/" class="avatar waves-effect waves-circle waves-light">
          <img src="/blog/img/user2.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">windy</h5>
          <a href="mailto:windynature@163.com" title="windynature@163.com" class="mail">windynature@163.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/blog/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                归档
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                分类
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/WindyNature" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://git.oschina.net/windynature" target="_blank" >
                <i class="icon icon-lg icon-mayun"></i>
                码云
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">mapbox源码分析（一）</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">mapbox源码分析（一）</h1>
        <h5 class="subtitle">
            
                <time datetime="2020-03-18T01:02:47.817Z" itemprop="datePublished" class="page-time">
  2020-03-18
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/mapbox源码分析/">mapbox源码分析</a></li></ul>

            
        </h5>
    </div>

    

</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#Mapbox原码分析"><span class="post-toc-text">Mapbox原码分析</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#一、基本架构"><span class="post-toc-text">一、基本架构</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#1-接口层"><span class="post-toc-text">1. 接口层</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Marker"><span class="post-toc-text">Marker</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Map"><span class="post-toc-text">Map</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Control"><span class="post-toc-text">Control</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#2-逻辑层"><span class="post-toc-text">2. 逻辑层</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Camera"><span class="post-toc-text">Camera</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Handler"><span class="post-toc-text">Handler</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#3-渲染模块"><span class="post-toc-text">3. 渲染模块</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#3-1-数据模块"><span class="post-toc-text">3.1 数据模块</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#3-1-1-Style"><span class="post-toc-text">3.1.1 Style</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#3-1-2-StyleLayer"><span class="post-toc-text">3.1.2 StyleLayer</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#3-1-3-SourceCache"><span class="post-toc-text">3.1.3 SourceCache</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#3-1-4-Transform"><span class="post-toc-text">3.1.4 Transform</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#3-1-5-Source"><span class="post-toc-text">3.1.5 Source</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#3-1-6-WrokerSource"><span class="post-toc-text">3.1.6 WrokerSource</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#3-1-7-Tile"><span class="post-toc-text">3.1.7 Tile</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#3-1-8-worker"><span class="post-toc-text">3.1.8 worker</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#3-1-9-bucket"><span class="post-toc-text">3.1.9 bucket</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#3-1-10-dispatcher"><span class="post-toc-text">3.1.10 dispatcher</span></a></li></ol></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#3-2-Painter"><span class="post-toc-text">3.2 Painter</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#3-2-1-draw"><span class="post-toc-text">3.2.1 draw</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#3-2-2-manager"><span class="post-toc-text">3.2.2 manager</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-6"><a class="post-toc-link" href="#3-2-2-1-ImageManager"><span class="post-toc-text">3.2.2.1 ImageManager</span></a></li><li class="post-toc-item post-toc-level-6"><a class="post-toc-link" href="#3-2-2-2-GlyphManager"><span class="post-toc-text">3.2.2.2 GlyphManager</span></a></li></ol></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#3-2-3-gl"><span class="post-toc-text">3.2.3 gl</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#3-2-4-programs"><span class="post-toc-text">3.2.4 programs</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#3-2-5-Shaders"><span class="post-toc-text">3.2.5 Shaders</span></a></li></ol></li></ol></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#二、数据处理流程"><span class="post-toc-text">二、数据处理流程</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#三、数据加载与渲染流程"><span class="post-toc-text">三、数据加载与渲染流程</span></a></li></ol></li></ol>
        </nav>
    </aside>
    
<article id="post-mapbox/mapbox源码分析"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">mapbox源码分析（一）</h1>
        <div class="post-meta">
            <time class="post-time" title="2020年03月18日 9:02" datetime="2020-03-18T01:02:47.817Z"  itemprop="datePublished">2020-03-18</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/mapbox源码分析/">mapbox源码分析</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h1 id="Mapbox原码分析"><a href="#Mapbox原码分析" class="headerlink" title="Mapbox原码分析"></a>Mapbox原码分析</h1><h2 id="一、基本架构"><a href="#一、基本架构" class="headerlink" title="一、基本架构"></a>一、基本架构</h2><figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="/blog/2020/03/18/mapbox/mapbox源码分析/基本架构.png" alt="基本架构" title>
                </div>
                <div class="image-caption">基本架构</div>
            </figure>


<h3 id="1-接口层"><a href="#1-接口层" class="headerlink" title="1. 接口层"></a>1. 接口层</h3><h4 id="Marker"><a href="#Marker" class="headerlink" title="Marker"></a>Marker</h4><p>在<code>Mapbox GL JS</code>中<code>Marker</code>类似与<code>openlayers</code>中的<code>overlay</code>，以DOM元素的方式呈现信息。默认的是一个浅蓝色的svg小图标。可以在marker的DOM元素中使用svg、其他dom元素、以及canvas呈现其他功能，方式较为灵活，例如做动画效果；其缺点是由于是dom结构的所以在数据量大的情况下渲染效率低。</p>
<p><strong>示例</strong></p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> marker = <span class="keyword">new</span> mapboxgl.Marker()</span><br><span class="line">	.setLngLat([<span class="number">104</span>,<span class="number">30</span>])</span><br><span class="line">	.addTo(map);</span><br></pre></td></tr></table></figure>

<h4 id="Map"><a href="#Map" class="headerlink" title="Map"></a>Map</h4><p><code>Mapbox GL JS</code>的承载对象，表示页面上的地图，公开了方法和属性，使得我们可以通过编码的方式来改变地图，并在用户与其交互的时候触发事件。p</p>
<p><strong>示例</strong></p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> map = <span class="keyword">new</span> mapboxgl.Map(&#123;</span><br><span class="line">  container: <span class="string">'map'</span>,</span><br><span class="line">  center: [<span class="number">-122.420679</span>, <span class="number">37.772537</span>],</span><br><span class="line">  zoom: <span class="number">13</span>,</span><br><span class="line">  style: style_object,</span><br><span class="line">  hash: <span class="literal">true</span>,</span><br><span class="line">  transformRequest: <span class="function">(<span class="params">url, resourceType</span>)=&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span>(resourceType === <span class="string">'Source'</span> &amp;&amp; url.startsWith(<span class="string">'http://myHost'</span>)) &#123;</span><br><span class="line">      <span class="keyword">return</span> &#123;</span><br><span class="line">       url: url.replace(<span class="string">'http'</span>, <span class="string">'https'</span>),</span><br><span class="line">       headers: &#123; <span class="string">'my-custom-header'</span>: <span class="literal">true</span>&#125;,</span><br><span class="line">       credentials: <span class="string">'include'</span>  <span class="comment">// Include cookies for cross-origin requests</span></span><br><span class="line">     &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<h4 id="Control"><a href="#Control" class="headerlink" title="Control"></a>Control</h4><p>控件类，与地图交互的控件，默认的有:</p>
<ul>
<li><code>NavigationControl</code>：导航控件</li>
<li><code>GeolocateControl</code>：地理定位控件</li>
<li><code>AttributionControl</code>：版权信息控件</li>
<li><code>ScaleControl</code>：缩放控件</li>
<li><code>FullscreenControl</code>：全屏控件</li>
</ul>
<h3 id="2-逻辑层"><a href="#2-逻辑层" class="headerlink" title="2. 逻辑层"></a>2. 逻辑层</h3><h4 id="Camera"><a href="#Camera" class="headerlink" title="Camera"></a>Camera</h4><p><code>Map</code>的父类，主要是控制地图视角的展示。</p>
<h4 id="Handler"><a href="#Handler" class="headerlink" title="Handler"></a>Handler</h4><p>与地图交互的处理函数，有鼠标交互，触摸交互和其他手势交互方式。</p>
<ul>
<li><code>BoxZoomHandler</code></li>
<li><code>ScrollZoomHandler</code></li>
<li><code>DragPanHandler</code></li>
<li><code>DragRotateHandler</code></li>
<li><code>KeyboardHandler</code></li>
<li><code>DoubleClickZoomHandler</code></li>
<li><code>TouchZoomRotateHandler</code></li>
</ul>
<h3 id="3-渲染模块"><a href="#3-渲染模块" class="headerlink" title="3. 渲染模块"></a>3. 渲染模块</h3><h4 id="3-1-数据模块"><a href="#3-1-数据模块" class="headerlink" title="3.1 数据模块"></a>3.1 数据模块</h4><h5 id="3-1-1-Style"><a href="#3-1-1-Style" class="headerlink" title="3.1.1 Style"></a>3.1.1 Style</h5><p><code>Mapbbox GL JS</code>中对样式的管理，source的加载及各个图层的加载。</p>
<h5 id="3-1-2-StyleLayer"><a href="#3-1-2-StyleLayer" class="headerlink" title="3.1.2 StyleLayer"></a>3.1.2 StyleLayer</h5><p>图层的样式类，其子类主要有</p>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="/blog/2020/03/18/mapbox/mapbox源码分析/StyleLayer子类.png" alt="StyleLayer子类" title>
                </div>
                <div class="image-caption">StyleLayer子类</div>
            </figure>

<h5 id="3-1-3-SourceCache"><a href="#3-1-3-SourceCache" class="headerlink" title="3.1.3 SourceCache"></a>3.1.3 SourceCache</h5><p><code>SourceCache</code>的主要职责有：</p>
<ul>
<li>创建<code>Source</code>的一个实例</li>
<li>转发<code>Source</code>的事件</li>
<li>缓存从<code>Source</code>中加载的<code>tile</code></li>
<li>加载指定视图窗口需要渲染的tile</li>
<li>将指定窗口不需要渲染的tile进行卸载</li>
</ul>
<h5 id="3-1-4-Transform"><a href="#3-1-4-Transform" class="headerlink" title="3.1.4 Transform"></a>3.1.4 Transform</h5><p>单个变换，通常用于单个瓦片的缩放、旋转和缩放。</p>
<h5 id="3-1-5-Source"><a href="#3-1-5-Source" class="headerlink" title="3.1.5 Source"></a>3.1.5 Source</h5><p>数据源<code>Source</code>接口必须由每个源类型实现，包括“核心”类型(“vector”、”raster”、”video”等)和所有定制的第三方类型。</p>
<p><strong>Source接口实现类：</strong></p>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="/blog/2020/03/18/mapbox/mapbox源码分析/source结构.png" alt title>
                </div>
                <div class="image-caption"></div>
            </figure>

<h5 id="3-1-6-WrokerSource"><a href="#3-1-6-WrokerSource" class="headerlink" title="3.1.6 WrokerSource"></a>3.1.6 WrokerSource</h5><p>主要负责数据的加载和解析。</p>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="/blog/2020/03/18/mapbox/mapbox源码分析/WrokerSource实现类.png" alt="WrokerSource实现类" title>
                </div>
                <div class="image-caption">WrokerSource实现类</div>
            </figure>

<h5 id="3-1-7-Tile"><a href="#3-1-7-Tile" class="headerlink" title="3.1.7 Tile"></a>3.1.7 Tile</h5><p><code>tile</code>对象是坐标的组合，坐标定义了它的位置，以及它的内容的惟一ID和数据跟踪。</p>
<h5 id="3-1-8-worker"><a href="#3-1-8-worker" class="headerlink" title="3.1.8 worker"></a>3.1.8 worker</h5><p>webworker线程，这里加载<code>tile</code>，放入对应图层的<code>source</code>中。</p>
<h5 id="3-1-9-bucket"><a href="#3-1-9-bucket" class="headerlink" title="3.1.9 bucket"></a>3.1.9 bucket</h5><p><strong><code>Bucket</code></strong>接口是将矢量切片转换为WebGL缓冲区的单点知识(<code>single point of knowledge</code>)。</p>
<p><strong><code>Bucket</code></strong>接口是一个抽象接口。在每个样式图层类型中都有实现，创建一个<code>bucket</code>可以通过<code>StyleLayer#createBucket</code>方法。</p>
<p>具体的<code>bucket</code>使用样式图层中的布局选项（<code>layout options</code>），将要素几何转换为顶点和索引数据，以供顶点着色器使用。它们（通过<code>ProgramConfiguration</code>）使用要素属性和缩放级别来填充数据驱动样式所需的属性。</p>
<p><code>Bucket</code>设计为构建在<code>worker</code>线程上，然后序列化并传输回主线程进行渲染。在<code>worker</code>线程，<code>Bucket</code>的顶点、索引和属性数据存储在<code>bucket.arrays：ArrayGroup</code>中。当一个<code>bucket</code>的数据被序列化并发送回主线程时，将被反序列化（使用<code>new Bucket（serializedBucketData</code>，数组数据现在存储在<code>bucket.buffers：BufferGroup</code>中，<code>BufferGroups</code>保存与<code>ArrayGroups</code>相同的数据，但是被WebGL调整为消费。</p>
<h5 id="3-1-10-dispatcher"><a href="#3-1-10-dispatcher" class="headerlink" title="3.1.10 dispatcher"></a>3.1.10 dispatcher</h5><p>分发器<code>Dispatcher</code>的职责是从<code>Source</code>发送消息给相关的<code>WorkerSource</code>。</p>
<h4 id="3-2-Painter"><a href="#3-2-Painter" class="headerlink" title="3.2 Painter"></a>3.2 Painter</h4><p>对webgl的封装，包含了各类draw的功能以及<code>ImageManager(图片管理)</code>、<code>GlyphManager(字体管理)</code>、<code>Texture(纹理)</code>等。</p>
<p>例如<code>draw</code>功能的封装。</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> draw = &#123;</span><br><span class="line">    symbol,</span><br><span class="line">    circle,</span><br><span class="line">    heatmap,</span><br><span class="line">    line,</span><br><span class="line">    fill,</span><br><span class="line">    <span class="string">'fill-extrusion'</span>: fillExtrusion,</span><br><span class="line">    hillshade,</span><br><span class="line">    raster,</span><br><span class="line">    background,</span><br><span class="line">    debug,</span><br><span class="line">    custom</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>在<code>painter#renderLayer()</code>中</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">renderLayer(painter: Painter, <span class="attr">sourceCache</span>: SourceCache, <span class="attr">layer</span>: StyleLayer, <span class="attr">coords</span>: <span class="built_in">Array</span>&lt;OverscaledTileID&gt;) &#123;</span><br><span class="line">    <span class="keyword">if</span> (layer.isHidden(<span class="keyword">this</span>.transform.zoom)) <span class="keyword">return</span>;</span><br><span class="line">    <span class="keyword">if</span> (layer.type !== <span class="string">'background'</span> &amp;&amp; layer.type !== <span class="string">'custom'</span> &amp;&amp; !coords.length) <span class="keyword">return</span>;</span><br><span class="line">    <span class="keyword">this</span>.id = layer.id;</span><br><span class="line">    <span class="comment">//根据具体的style类型来绘制图层</span></span><br><span class="line">    draw[layer.type](painter, sourceCache, layer, coords, <span class="keyword">this</span>.style.placement.variableOffsets);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="3-2-1-draw"><a href="#3-2-1-draw" class="headerlink" title="3.2.1 draw"></a>3.2.1 draw</h5><blockquote>
<p>src-&gt;render</p>
</blockquote>
<p>封装了各图层的绘制功能，在源码的render目录下封装了各中方式的绘制。</p>
<ul>
<li>draw_background.js</li>
<li>draw_circle.js</li>
<li>draw_collision_debug.js</li>
<li>draw_custom.js</li>
<li>draw_debug.js</li>
<li>draw_fill_extrusion.js</li>
<li>draw_fill.js</li>
<li>draw_heatmap.js</li>
<li>draw_hillshade.js</li>
<li>draw_line.js</li>
<li>draw_raster.js</li>
<li>draw_symbol.js</li>
</ul>
<h5 id="3-2-2-manager"><a href="#3-2-2-manager" class="headerlink" title="3.2.2 manager"></a>3.2.2 manager</h5><blockquote>
<p>src-&gt;render</p>
</blockquote>
<p>主要有<code>ImageManager</code>和<code>GlyphManager</code>。</p>
<h6 id="3-2-2-1-ImageManager"><a href="#3-2-2-1-ImageManager" class="headerlink" title="3.2.2.1 ImageManager"></a>3.2.2.1 ImageManager</h6><p>在<code>mapbox-gl</code>中，添加一个图标的基本流程如下。</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//添加image</span></span><br><span class="line">map.loadImage(<span class="string">'imageUrl'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">error, image</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (error) <span class="keyword">throw</span> error;</span><br><span class="line">    map.addImage(<span class="string">'cameraIcon'</span>, image);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"> map.addLayer(&#123;</span><br><span class="line">     <span class="string">"id"</span>: <span class="string">"eventDetailLayer"</span>,</span><br><span class="line">     <span class="string">"type"</span>: <span class="string">"symbol"</span>,</span><br><span class="line">     <span class="string">"source"</span>: <span class="string">"eventDetail"</span>,</span><br><span class="line">     <span class="string">"layout"</span>: &#123;</span><br><span class="line">         <span class="string">"visibility"</span>: <span class="string">'none'</span>,</span><br><span class="line">         <span class="string">"icon-image"</span>: <span class="string">"cameraIcon"</span>, <span class="comment">//指定image</span></span><br><span class="line">         <span class="string">"icon-size"</span>: <span class="number">1</span>,</span><br><span class="line">         <span class="string">"icon-offset"</span>: [<span class="number">0</span>, <span class="number">-14</span>] <span class="comment">//设置偏移量</span></span><br><span class="line">     &#125;,</span><br><span class="line"> &#125;);</span><br></pre></td></tr></table></figure>

<p>在<code>mapbox-gl</code>的源码中，<code>ui/map.js</code>中封装了<code>addImage()</code>方法，改方法实际是调用了<code>style/style.js</code>中的<code>addImage()</code>方法。<code>style/style.js</code>中的<code>addImage()</code>方法如下：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//style/sytle.js中的addImage方法</span></span><br><span class="line">addImage(id: string, <span class="attr">image</span>: StyleImage) &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">this</span>.getImage(id)) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.fire(<span class="keyword">new</span> ErrorEvent(<span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">'An image with this name already exists.'</span>)));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">this</span>.imageManager.addImage(id, image);</span><br><span class="line">    <span class="keyword">this</span>.fire(<span class="keyword">new</span> Event(<span class="string">'data'</span>, &#123;<span class="attr">dataType</span>: <span class="string">'style'</span>&#125;));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//render/image_manager.js中的addImage方法</span></span><br><span class="line"> addImage(id: string, <span class="attr">image</span>: StyleImage) &#123;</span><br><span class="line">     assert(!<span class="keyword">this</span>.images[id]);</span><br><span class="line">     <span class="keyword">this</span>.images[id] = image;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>

<p>以上代码可见，在<code>mapbox-gl</code>的api中添加图片是调用了样式中的addImage()方法，其中样式中的图片是由<code>ImageManager</code>在做管理。<code>ImageManager</code>中内置了图片数组对添加的图片进行缓存。</p>
<p>在<code>mapbo-gl</code>的api中描述如下</p>
<blockquote>
<p>Add an image to the style. This image can be used in <code>icon-image</code>, <code>background-pattern</code>, <code>fill-pattern</code>, and <code>line-pattern</code>. An <a href="https://docs.mapbox.com/mapbox-gl-js/api/Map#error" target="_blank" rel="noopener">Map#error</a> event will be fired if there is not enough space in the sprite to add this image.</p>
</blockquote>
<p>可见这些图片可用做图标、背景填充、多边形填充以及线填充。</p>
<h6 id="3-2-2-2-GlyphManager"><a href="#3-2-2-2-GlyphManager" class="headerlink" title="3.2.2.2 GlyphManager"></a>3.2.2.2 GlyphManager</h6><p>矢量切片中字体的管理</p>
<h5 id="3-2-3-gl"><a href="#3-2-3-gl" class="headerlink" title="3.2.3 gl"></a>3.2.3 gl</h5><p>对webgl渲染过程及参数的一些封装。</p>
<h5 id="3-2-4-programs"><a href="#3-2-4-programs" class="headerlink" title="3.2.4 programs"></a>3.2.4 programs</h5><p>渲染需要的变量等的封装。</p>
<h5 id="3-2-5-Shaders"><a href="#3-2-5-Shaders" class="headerlink" title="3.2.5 Shaders"></a>3.2.5 Shaders</h5><p>着色器封装。</p>
<h2 id="二、数据处理流程"><a href="#二、数据处理流程" class="headerlink" title="二、数据处理流程"></a>二、数据处理流程</h2><figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="/blog/2020/03/18/mapbox/mapbox源码分析/数据处理流程.png" alt="数据处理流程" title>
                </div>
                <div class="image-caption">数据处理流程</div>
            </figure>

<h2 id="三、数据加载与渲染流程"><a href="#三、数据加载与渲染流程" class="headerlink" title="三、数据加载与渲染流程"></a>三、数据加载与渲染流程</h2><figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="/blog/2020/03/18/mapbox/mapbox源码分析/数据加载与渲染流程.png" alt="数据加载与渲染流程" title>
                </div>
                <div class="image-caption">数据加载与渲染流程</div>
            </figure>


<p><strong>参考文章</strong></p>
<ol>
<li><a href="https://www.cnblogs.com/dojo-lzz/p/10165817.html" target="_blank" rel="noopener">mapbox.gl源码解析——基本架构与数据渲染流程</a></li>
</ol>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2020-03-18T01:06:47.494Z" itemprop="dateUpdated">2020年3月18日 9:06</time>
</span><br>


        这里写留言或版权声明：<a href="/blog/2020/03/18/mapbox/mapbox源码分析/" target="_blank" rel="external">http://windynature.oschina.io/2020/03/18/mapbox/mapbox源码分析/</a>
    </div>
    <footer>
        <a href="http://windynature.oschina.io">
            <img src="/blog/img/user2.jpg" alt="windy">
            windy
        </a>
    </footer>
</blockquote>

        


        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/gis/">gis</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/mapbox-gl-js/">mapbox-gl-js</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://windynature.oschina.io/2020/03/18/mapbox/mapbox源码分析/&title=《mapbox源码分析（一）》 — 奔跑的蜗牛&pic=http://windynature.oschina.io/img/user2.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://windynature.oschina.io/2020/03/18/mapbox/mapbox源码分析/&title=《mapbox源码分析（一）》 — 奔跑的蜗牛&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://windynature.oschina.io/2020/03/18/mapbox/mapbox源码分析/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《mapbox源码分析（一）》 — 奔跑的蜗牛&url=http://windynature.oschina.io/2020/03/18/mapbox/mapbox源码分析/&via=http://windynature.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://windynature.oschina.io/2020/03/18/mapbox/mapbox源码分析/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/blog/2020/03/18/gis基本原理/瓦片计算工具类/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">切片请求工具类</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/blog/2020/03/17/gis基本原理/瓦片请求原理/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">瓦片请求原理</h4>
      </a>
    </div>
  
</nav>



    

<div class="comments" id="comments">
    <div class="ds-thread" data-thread-key="mapbox/mapbox源码分析" data-title="mapbox源码分析（一）" data-url="http://windynature.oschina.io/2020/03/18/mapbox/mapbox源码分析/"></div>
</div>
<script>
lazyScripts.push('//cdn.bootcss.com/marked/0.3.6/marked.min.js');

var duoshuoQuery = {short_name:'ysblog', theme: 'none'};
lazyScripts.push('//unpkg.com/hexo-theme-material-indigo@1.5.2/js/embed.min.js');


</script>










</article>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            <span>博客内容遵循 <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p>
            <span>Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a></span>
            <span>奔跑的蜗牛 &copy; 2015 - 2020</span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://windynature.oschina.io/2020/03/18/mapbox/mapbox源码分析/&title=《mapbox源码分析（一）》 — 奔跑的蜗牛&pic=http://windynature.oschina.io/img/user2.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://windynature.oschina.io/2020/03/18/mapbox/mapbox源码分析/&title=《mapbox源码分析（一）》 — 奔跑的蜗牛&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://windynature.oschina.io/2020/03/18/mapbox/mapbox源码分析/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《mapbox源码分析（一）》 — 奔跑的蜗牛&url=http://windynature.oschina.io/2020/03/18/mapbox/mapbox源码分析/&via=http://windynature.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://windynature.oschina.io/2020/03/18/mapbox/mapbox源码分析/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/blog/', SHARE: true, REWARD: false };



</script>

<script src="//unpkg.com/hexo-theme-material-indigo@1.5.2/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@1.5.2/js/search.min.js" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
